//
//  TokenDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/8/2.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "TokenDetailViewController.h"
#import "ResizeView.h"
#import "HttpTokenAction.h"
#import "TransHistoryViewController.h"

@interface TokenDetailViewController (){
    TokenDetailBlock tokenDetailBlock;
}
@property (nonatomic,strong) NSString *titleN;
@property (nonatomic,strong) NSString *totalNum;
@property (nonatomic,strong) UITextField *amountTF;
@property (nonatomic,strong) UISlider *valueSlier;

@end

@implementation TokenDetailViewController

-(id)initWithTitle:(NSString*)title withNumber:(NSString*)number Block:(TokenDetailBlock)block{
    if(self == [super init]){
        self.titleN = title;
        self.totalNum = number;
        tokenDetailBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = self.titleN;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightbutton setTitle:@"记录" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(historyAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];
    [resizeView addResizeSubview:[self getTitleResizeViewInfo]];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:10]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo]];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];
    [resizeView addResizeSubview:[self getCloseResizeViewInfo]];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:10]];
    [resizeView addResizeSubview:[self getBarResizeViewInfo]];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:40]];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
}

-(void)historyAction:(id)sender{
    TransHistoryViewController *transHistoryViewController = [[TransHistoryViewController alloc] init];
    [self.navigationController pushViewController:transHistoryViewController animated:YES];
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    return resizeViewInfo;
    
}

-(ResizeViewInfo*)getTitleResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 20;
    
    UILabel *label = [UILabel new];
    if ([self.titleN isEqualToString:@"可用"]) {
        label.text = @"可用(key):";

    }else{
        label.text = @"已锁定(key):";

    }
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor grayColor];
    [resizeViewInfo addSubview:label];
    label.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 30;
    
    UILabel *label = [UILabel new];
    label.text = self.totalNum;
    label.font = [UIFont systemFontOfSize:25];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    [resizeViewInfo addSubview:label];
    label.sd_layout.leftSpaceToView(resizeViewInfo.view, 0).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0);
    
    return resizeViewInfo;
}

-(void)textchange:(id)sender{
    UITextField *textField = sender;
    self.valueSlier.value = [textField.text floatValue];
}

-(ResizeViewInfo*)getCloseResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 45;
    
    UITextField *textField = [UITextField new];
    textField.placeholder = @"锁定数量";
    [textField addTarget:self action:@selector(textchange:) forControlEvents:UIControlEventEditingChanged];
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    [resizeViewInfo addSubview:textField];
    textField.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 15);
    self.amountTF = textField;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 15).heightIs(1);
    return resizeViewInfo;
}

-(ResizeViewInfo*)getBarResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UISlider *slider = [UISlider new];
    slider.maximumValue = [self.totalNum floatValue];
    slider.minimumValue = 0.0f;
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [resizeViewInfo addSubview:slider];
    slider.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 15);
    self.valueSlier = slider;
    return resizeViewInfo;
}

-(void)sliderAction:(id)sender{
    UISlider *slider = sender;
    self.amountTF.text = [NSString stringWithFormat:@"%.2f",slider.value];
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    if ([self.titleN isEqualToString:@"可用"]) {
        [submitButton setTitle:@"锁定" forState:UIControlStateNormal];
    
    }else{
        [submitButton setTitle:@"解锁" forState:UIControlStateNormal];

    }

    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.amountTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入数量" autoDissmissAfterDelay:0];
        return;
    }
    TokenStatusActionInfo *tokenStatusActionInfo = [[TokenStatusActionInfo alloc] init];
    tokenStatusActionInfo.acctId = self.tokenInfo.id;
    
    if ([self.titleN isEqualToString:@"可用"]) {
        tokenStatusActionInfo.changeType = @"toLock";
    }else{
        tokenStatusActionInfo.changeType = @"toDeblocking";

    }
    tokenStatusActionInfo.changeAmount = self.amountTF.text;
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpTokenAction postChangeTokenStatus:tokenStatusActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if(!error){
            if ([self.titleN isEqualToString:@"可用"]) {
                [self showMessage:@"锁定成功" autoDissmissAfterDelay:0];

            }else{
                [self showMessage:@"解锁成功" autoDissmissAfterDelay:0];

            }
            
            if(tokenDetailBlock){
                tokenDetailBlock();
            }
            
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
    }];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
